$(function(){
    getUserInfo()
    // const layer = layui.layer
    $('.login-out').on('click',function(){
        layer.confirm('是否退出',{icon : 3 ,title : '提示'},function(index){
            location.href = '/login.html'

            localStorage.removeItem('token')
            layer.close(index)
        })
    })
})
function getUserInfo(){
    $.ajax({
        type : 'GET',
        url : '/my/userinfo',
        headers:{
            Authorization : localStorage.token || ''
        },
        success(res){
            console.log(res);
            if(res.status !== 0){
                return
            }
            renderAvatar(res.data)
            
        },
        complete : function(e){
            console.log(e);
            if(e.responseJSON.status ===1 && e.responseJSON.message ==='身份认证失败！') {
                // 清除本地存储
                localStorage.removeItem('token')
                // 跳转登录页面
                location.href ='/login.html' 
            }
        }
    })

}
// function renderAvatar(data){
//     const name = data.nickname || data.username
//     if(data.user_pic !== null){
//         $('.layui-nav-img').attr('src',data.user_pic).show()
//         $('.text-avatar').hide()
//     }else{
//         $('.layui-nav-img').hide()
//         var first = name[0].toUpperCase()
//         $('.text-avatar').html(first).show()
// }
// }
function renderAvatar(data){
    console.log(data)
    const name = data.nickname || data.username
    if(data.user_pic !==  null){
        $('.layui-nav-img').attr('src',data.user_pic).show()
        $('.text-avatar').hide()
    }else{
        $('.layui-nav-img').hide()
        var first = name[0].toUpperCase()
        $('.text-avatar').html(first).show()
    }
}